<?php

namespace app\model\system;

use app\model\BaseModel;
use albert\traits\ModelTrait;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 配置分类 Model
 * Class SystemConfigTab
 * @package app\models\system
 */
class SystemConfigTab extends BaseModel
{
    use ModelTrait;

    /**
     * 初始化查询条件
     * Author: Albert3306
     * Date: 2021/6/30
     * @param $where
     * @param null $model
     * @return SystemConfigTab|mixed
     */
    public static function initWhere($where, $model = null)
    {
        $model = $model ?? new self();
        if (isset($where['pid']) && $where['pid'] !== '') {
            $model = $model->where('pid', $where['pid']);
        }
        if (isset($where['type']) && $where['type'] !== '') {
            $model = $model->where('type', $where['type']);
        }
        if (isset($where['status']) && $where['status'] !== '') {
            $model = $model->where('status', $where['status']);
        }
        return $model;
    }

    /**
     * 获取配置分类
     * @param array $where
     * @return array
     * @throws DbException
     */
    public static function getSystemConfigTabPage(array $where = []): array
    {
        $model = new self;
        if ($where['title'] != '') $model = $model->where('title', 'LIKE', "%$where[title]%");
        if ($where['status'] != '') $model = $model->where('status', $where['status']);
        $model = $model->where('pid', $where['pid']);
        return self::page($model, $where);

    }

    /**
     * 查询配置分类树结构数据
     * Author: Albert3306
     * Date: 2021/6/30
     * @return array[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getTabTree(): array
    {
        $tabList = self::initWhere([])->field('id,pid,title')->select()->toArray();
        $list = sort_list_tier($tabList);
        $tabs = [['value' => 0, 'label' => '顶级分类']];
        foreach ($list as $tab) {
            $tabs[] = ['value' => $tab['id'], 'label' => $tab['html'] . $tab['title']];
        }
        return $tabs;
    }

    /**
     * 获取单选按钮或者多选按钮的显示值
     * @param $menu_name
     * @param $value
     * @return string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getRadioOrCheckboxValueInfo($menu_name, $value): string
    {
        $option = [];
        $config_one = SystemConfig::getOneConfig('menu_name', $menu_name);
        $parameter = explode("\n", $config_one['parameter']);
        foreach ($parameter as $v) {
            if (isset($v) && strlen($v) > 0) {
                $data = explode('=>', $v);
                $option[$data[0]] = $data[1];
            }
        }
        $str = '';
        if (is_array($value)) {
            foreach ($value as $v) {
                $str .= $option[$v] . ',';
            }
        } else {
            $str .= !empty($value) ? $option[$value] : $option[0];
        }
        return $str;
    }

    /**
     * 获取全部
     * @param int $type
     * @return Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getAll(int $type = 0): Collection
    {
        $where['pid'] = 0;
        $where['status'] = 1;
        if ($type > -1) $where['type'] = $type;
        return self::initWhere($where)->order('sort desc,id asc')->select();
    }

    /**
     * 获取指定分类所有子分类
     * @param $pid
     * @return Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getChildrenTab($pid): Collection
    {
        $where['pid'] = $pid;
        $where['status'] = 1;
        return self::initWhere($where)->order('sort desc,id asc')->select();
    }
}